//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================


#include <ddk.h>
#include <eltypes.h>
#include <stdio.h>
#include <stdlib.h>
#include "xf86DCConf.h"
#include "gdivga.h"
/*
 * findConfigFile --
 *     Locate the XF86Config file.  Abort if not found.
 */

wchar_t* cfgPath[] = {
     L"/tfs/svga.cfg",
     L"./svga.cfg",
     L"/bin/svga.cfg"
    };

void
findConfigFile(char *filename, FILE **fp)
{
#if 0//not used//
    if (filename == NULL)   return;

    size_t len;
    for (int i = 0; i < 3; i++) {
        if ((*fp =_wfopen(cfgPath[i], L"r")) != NULL) {
            len = wcslen(cfgPath[i]);
            WcsToMbs(filename, cfgPath[i], len + 1); //BUGBUG: must know the len of 'filename'
            kprintf("XF86Config: %s\n", filename);
            kprintf("%s stands for supplied, %s stands for probed/default values\n", \
                XCONFIG_GIVEN, XCONFIG_PROBED);
            return;
        }
    }
    //find from the dir which d3d8.dll existed
    IProcess *pProcess = NULL;
    GetCurrentProcess(&pProcess);
    IModule *pModule = NULL;
    ECode ec = pProcess->GetModuleByName(L"d3d8.dll", &pModule);
    if (FAILED(ec)) {
        kprintf("Failed to execute GetModuleByName!\n");
        return;
    }

    WStringBuf_<_MAX_PATH> fileName;
    ec = pModule->GetFileName(fileName);
    if (FAILED(ec)) {
        kprintf("Get Module File Name failed! \n");
        return;
    }

    len = fileName.Length();
    wcscpy((wchar_t*)fileName + len - 8, L"svga.cfg");

    if ((*fp = _wfopen((wchar_t*)fileName, L"r")) == NULL) {
        kprintf("Could not find config file!\n");
        return;
    }
    //to convert right, must add 1 ,that is WcsToMbs needed
    WcsToMbs(filename, (wchar_t*)fileName, len + 1); //BUGBUG: must know the len of 'filename'
    kprintf("XF86Config: %s\n", filename);
    kprintf("%s stands for supplied, %s stands for probed/default values\n", \
        XCONFIG_GIVEN, XCONFIG_PROBED);
#endif
}
